<?php
/**
* Table helper class for EICms
*
* @author hcderaad
* @package EventICms
*/

/**
* Table helper class for EICms
*
* @author hcderaad
* @package EventICms
*/
class EICms_Table extends Zend_Db_Table {

	/**
	* Inserts the data.
	*
	* @param array $data The data to insert.
	* @return void
	*/
	public function insert(array $data) {
		$this->log('insert',$data);
		return parent::insert($data); 
	}
	
	/**
	* Updates a record in the database with the given value.
	*
	* @param array $data The data to update the record(s) with
	* @param string|array $where The where clause to specify which data to update.
	* @return void
	*/
	public function update(array $data,$where) {
		$logData = $this->fetchRow($where)->toArray();
		$logData = array_merge($logData,$data);
		$this->log('update',$logData);
		return parent::update($data,$where);
	}
	
	/**
	* Deletes a record from a table in the database.
	*
	* @param string|array $where The where clause to specify which data to delete.
	* @return void
	*/
	public function delete($where) {
		$this->log('delete',$this->fetchRow($where)->toArray());
		return parent::delete($where);
	}
	
	/**
	* Logs a database operation to the log table.
	*
	* @param string $action The action that has been performed (insert, update or delete).
	* @param array $data The records data.
	* @return void
	* @uses EICms_User
	*/
	protected function log($action, array $data) {
		if($this->_name != 'log') {
			$log = new EICms_Table_Log();
			$logRow = $log->createRow(array('table' => $this->_name,
										'action' => $action,
										'record' => $data['id'],
										'user' => EICms_Auth::getInstance()->getIdentity()->id,
										'content' => Zend_Json::encode($data),
										'datetime' => date('Y-m-d H:i:s'),
										'timestamp' => time()));
			$logRow->save();
		}
		return null;
	}
	
	/**
	 * Creates an empty Row.
	 *
	 * @param array $data
	 * @return Zend_Db_Table_Row_Abstract
	 */
	public function createRow(array $data = array()) {
		$row = parent::createRow($data);
		$row->id = $this->createId();
		return $row;
	}
	
	/**
	 * Creates a Globally Unique IDentifier hash.
	 *
	 * @return string
	 */
	protected function createId() {
		return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
        mt_rand( 0, 0x0fff ) | 0x4000,
        mt_rand( 0, 0x3fff ) | 0x8000,
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
	}
	
	/**
	 * Returns the tablename
	 *
	 * @return string
	 */
	public function getTableName() {
		return $this->_name;
	}
	
	/**
	 * Returns the listfields for this table.
	 *
	 * @return array
	 */
	public function getListFields() {
		return $this->_listFields;
	}
	
	/**
	 * Returns the primary key
	 *
	 * @return string|array
	 */
	public function getPrimary() {
		return $this->_primary;
	}
}

?>